@echo off
chcp 65001

::打包上传服务启停一条龙
echo.
echo.
echo 请注意，此脚本需要使用到putty，请去官网下载并安装！！！
echo.
set /p var=请确认您已经配置好本地文件与服务器相关信息(y/n)：
if %var% neq y (exit)

::服务器ip
set SERVER_ADDRESS=127.0.0.1
::用户名
set SERVER_USERNAME=root
::密码
set SERVER_PASSWORD=123456
::服务器存放jar的位置
set TARGET_DIR=/your/target/dir

::本地代码位置
set LOCAL_CODE_DIR=D:\your\project\dir
::打包完成后jar包位置
set LOCAL_JAR_DIR=%LOCAL_CODE_DIR%\target\

::盘符,根据本地代码位置自动获取
set LOCAL_DISK_SYMBOL=%LOCAL_CODE_DIR:~0,2%

call %LOCAL_DISK_SYMBOL%
call cd %LOCAL_CODE_DIR%

::打包
call mvn clean package
echo.
echo.
if %errorlevel% equ 0 (
	echo ==================打包成功===================
) else (
	echo 打包失败,单击任意键退出
	pause
)
pause

::上传
call cd %LOCAL_JAR_DIR%
call dir
echo.
echo.
set /p JAR_FILE=请选择要上传的jar:
echo.
echo 正在上传%JAR_FILE%至%SERVER_ADDRESS%:%TARGET_DIR%
echo.
echo y | pscp -pw %SERVER_PASSWORD% %LOCAL_JAR_DIR%%JAR_FILE% %SERVER_USERNAME%@%SERVER_ADDRESS%:%TARGET_DIR%
echo.
echo %JAR_FILE%已经上传至%SERVER_ADDRESS%:%TARGET_DIR%
echo.
pause

::停止旧服务
echo 正在停止当前版本的java服务
echo y | plink %SERVER_USERNAME%@%SERVER_ADDRESS% -pw %SERVER_PASSWORD% "cd %TARGET_DIR%;bash ./server_ctrl.sh stop"%
echo.
echo 服务已停止
echo.
pause

::启动新服务
echo "正在启动新服务"
echo.
echo y | plink %SERVER_USERNAME%@%SERVER_ADDRESS% -pw %SERVER_PASSWORD% "cd %TARGET_DIR%;bash ./server_ctrl.sh start %JAR_FILE%"%
echo.
echo "服务%JAR_FILE%已启动"

pause